package com.jinniulive.platform.module.live.fragments;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.Spinner;

import com.jinniulive.platform.R;
import com.jinniulive.platform.adapter.GiftBaseAdapter;
import com.jinniulive.platform.adapter.GiftViewPager;
import com.jinniulive.platform.bean.UserManageInfo;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class GiftFragment extends Fragment implements View.OnClickListener {



    public GiftFragment() {
        // Required empty public constructor
    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    private ViewPager mViewPager;
    private TabLayout mTabLayout;
    private List<Fragment> mFragments;
    private GiftViewPager mGiftPagerAdapter;
    private List<Integer> mListUserId=new ArrayList<>();
    private List<String> mListName=new ArrayList<>();
    private List<String> mListNumber=new ArrayList<>();
    private Spinner mSpinnerUser,mSpinnerNumber;
    private GiftBaseAdapter mBaseAdapter,mBaseAdapter2;
    private ImageButton mButtonGive;
    private int userId;
    private int numberPosition;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_gift, container, false);
        initView(view);
        initData();
        setData();
        setListener();
        return view;
    }

    private void setListener() {
        mSpinnerUser.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                userId= mListUserId.get(position);
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        mSpinnerNumber.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                numberPosition=position;
            }

            @Override
            public void onNothingSelected(AdapterView<?> parent) {

            }
        });
        mButtonGive.setOnClickListener(this);

    }

    private void setData() {
        mBaseAdapter=new GiftBaseAdapter(getContext(),mListName);
        mBaseAdapter2=new GiftBaseAdapter(getContext(),mListNumber);
        mSpinnerUser.setAdapter(mBaseAdapter);
        mSpinnerNumber.setAdapter(mBaseAdapter2);
        mListNumber.add("1");
        mBaseAdapter2.notifyDataSetChanged();
        mListNumber.add("666");
        mListNumber.add("999");
        mListNumber.add("1082");
        mBaseAdapter2.notifyDataSetChanged();

    }

    private void initData() {
        mFragments=new ArrayList<>();
        mFragments.add(new SubGitfFragment());
        mFragments.add(new Sub2GitfFragment());
        mGiftPagerAdapter=new GiftViewPager(getChildFragmentManager(),getContext(),mFragments);
        mViewPager.setAdapter(mGiftPagerAdapter);
        mTabLayout.setupWithViewPager(mViewPager);

    }

    private void initView(View view) {
        mViewPager = (ViewPager) view.findViewById(R.id.fragment_gift_vp);
        mTabLayout = (TabLayout) view.findViewById(R.id.fragment_gift_tl);
        mSpinnerUser = (Spinner) view.findViewById(R.id.fragment_gift_sp_user);
        mSpinnerNumber = (Spinner) view.findViewById(R.id.fragment_gift_sp_number);
        mButtonGive = (ImageButton) view.findViewById(R.id.fragment_gift_ib);
    }

    @Subscribe
    public void receive(UserManageInfo info){
        int userId = info.userId;
        String nickName = info.nickName;
        mListName.add(nickName);
        mListUserId.add(userId);
        mBaseAdapter.notifyDataSetChanged();
    }


    @Override
    public void onClick(View v) {
        if (v != null) {
            switch (v.getId()){
                case R.id.fragment_gift_ib:

                    break;

            }
        }
    }
}
